package com.brspace.qualitycontrol.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.brspace.qualitycontrol.entity.Engineering;
import com.brspace.qualitycontrol.entity.vo.EngineeringVo;
import com.brspace.qualitycontrol.mapper.EngineeringMapper;
import com.brspace.qualitycontrol.service.EngineeringService;
import com.brspace.qualitycontrol.utils.EmptyUtil;
import com.brspace.qualitycontrol.utils.R;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 工程表 服务实现类
 * </p>
 *
 * @author ${author}
 * @since 2021-04-05
 */
@Service
public class EngineeringServiceImpl extends ServiceImpl<EngineeringMapper, Engineering> implements EngineeringService {
    @Resource
    private EngineeringMapper engineeringMapper;
    /**
     * 新增项目建设
     * @param engineering
     * @return
     */
    @Override
    public R addEng(Engineering engineering) {
        if (engineering==null){
            throw new SecurityException("数据为空");
        }
        QueryWrapper<Engineering> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("project_name",engineering.getProjectName());
        Engineering engineerings = engineeringMapper.selectOne(queryWrapper);
        System.err.println(engineerings);
        if(engineerings!=null){
            throw new SecurityException("工程单位已存在！");
        }
        if (EmptyUtil.isEmpty(engineering.getOwnerUnit())) {
            throw new SecurityException("业主单位为空");
        }
        if (EmptyUtil.isEmpty(engineering.getProjectName())) {
            throw new SecurityException("工程名称为空");
        }
        if (EmptyUtil.isEmpty(engineering.getBidWinner())) {
            throw new SecurityException("中标单位为空");
        }
        if (EmptyUtil.isEmpty(engineering.getProjectNumber())) {
            throw new SecurityException("项目编号为空");
        }
        if (engineeringMapper.insert(engineering)>0) {
            return R.ok().message("成功");
        }
        return R.error().message("失败");
    }

    /**
     * 分页信息
     * @param pageParam
     * @param engineeringVo
     * @return
     */
    @Override
    public IPage<Engineering> selectPage(Page<Engineering> pageParam, EngineeringVo engineeringVo) {
        QueryWrapper<Engineering> queryWrapper = new QueryWrapper<>();
        if (engineeringVo==null) {
            return baseMapper.selectPage(pageParam,queryWrapper);
        }
        // 按条件查询
        String ownerUnit = engineeringVo.getOwnerUnit();
        String projectName = engineeringVo.getProjectName();
        String startTime = engineeringVo.getStartTime();
        String completeTime = engineeringVo.getCompleteTime();
        if (!EmptyUtil.isEmpty(ownerUnit)) {
            queryWrapper.likeRight("owner_unit",ownerUnit);
        }
        if (!EmptyUtil.isEmpty(projectName)) {
            queryWrapper.likeRight("project_name",projectName);
        }
        if (!EmptyUtil.isEmpty(startTime)) {
            queryWrapper.likeRight("start_time",startTime);
        }
        if (!EmptyUtil.isEmpty(completeTime)) {
            queryWrapper.likeRight("complete_time",completeTime);
        }
        return baseMapper.selectPage(pageParam,queryWrapper);
    }

    @Override
    public EngineeringVo getTotle() {
        return new EngineeringVo().getinfo(baseMapper.selectList(null).size(),baseMapper.selectSum(),baseMapper.selectSum());
    }

    @Override
    public List<Integer> getChart() {
        List<Integer> arrayList = new ArrayList<>();
        for (int a = 1;a<5;a++){
            arrayList.add(baseMapper.selectCount(new QueryWrapper<Engineering>().eq("bidding_type",a)));
        }
        return arrayList;
    }
}
